using System;
using System.Windows.Forms;
using AocBossTimer.Timer;

namespace AocBossTimer
{
    public partial class ConfigForm : Form
    {
        private readonly ITimerConfigurable _configurable;
        private IBossTimer _currentTimer;

        public ConfigForm(IBossTimer currentTimer, ITimerConfigurable configurable) : this()
        {
            _currentTimer = currentTimer;
            _configurable = configurable;
            nudFontsize.Value = configurable.Fontsize;
            UpdateCustomFields();
            if (currentTimer.GetType().Equals(typeof (VistrixTimer)))
            {
                rbtVistrix.Checked = true;
            }
            else if (currentTimer.GetType().Equals(typeof (ExcorantTimer)))
            {
                rbtExcorant.Checked = true;
            }
            else if (currentTimer.GetType().Equals(typeof (AhazuZagemTimer)))
            {
                rbtAhazuZagem.Checked = true;
            }
            else if (currentTimer.GetType().Equals(typeof (ChathaTimer)))
            {
                rbtChatha.Checked = true;
            }
            else if (currentTimer.GetType().Equals(typeof (CustomTimer)))
            {
                rbtCustom.Checked = true;
            }
        }

        public ConfigForm()
        {
            InitializeComponent();
        }

        private void rbtVistrix_CheckedChanged(object sender, EventArgs e)
        {
            EnableCustomFields(false);
            _currentTimer = new VistrixTimer();
            UpdateCustomFields();
        }

        private void UpdateCustomFields()
        {
            txtTimer.Text = _currentTimer.StartTimer.ToString();
            txtOrange.Text = _currentTimer.OrangeTimer.ToString();
            txtRot.Text = _currentTimer.RedTimer.ToString();
        }

        private void EnableCustomFields(bool enabled)
        {
            txtTimer.Enabled = enabled;
            txtOrange.Enabled = enabled;
            txtRot.Enabled = enabled;
        }

        private void rbtExcorant_CheckedChanged(object sender, EventArgs e)
        {
            EnableCustomFields(false);
            _currentTimer = new ExcorantTimer();
            UpdateCustomFields();
        }

        private void rbtAhazuZagem_CheckedChanged(object sender, EventArgs e)
        {
            EnableCustomFields(false);
            _currentTimer = new AhazuZagemTimer();
            UpdateCustomFields();
        }

        private void rbtChatha_CheckedChanged(object sender, EventArgs e)
        {
            EnableCustomFields(false);
            _currentTimer = new ChathaTimer();
            UpdateCustomFields();
        }

        private void rbtCustom_CheckedChanged(object sender, EventArgs e)
        {
            EnableCustomFields(true);
        }

        private void CreateCustomTimer()
        {
            _currentTimer = new CustomTimer(Convert.ToInt32(txtTimer.Text), Convert.ToInt32(txtOrange.Text),
                                            Convert.ToInt32(txtRot.Text));
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (rbtCustom.Checked)
            {
                CreateCustomTimer();
            }
            _configurable.Timer = _currentTimer;
            _configurable.Fontsize = Convert.ToInt32(nudFontsize.Value);
            Hide();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }
    }

    public interface ITimerConfigurable
    {
        IBossTimer Timer { get; set; }
        int Fontsize { get; set; }
    }
}